package com.hfy.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hfy.model.Es.PostES;
import com.hfy.model.po.user.post.PostPo;
import com.hfy.model.vo.post.IndexShowPostVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 韩
 * @since 2024-11-28
 */
@Mapper
public interface PostMapper extends BaseMapper<PostPo> {

    @Select("SELECT p.id id,IF(p.is_nameless = TRUE , p.avatar, u.avatar) avatar, IF(p.is_nameless = TRUE , p.nickname, u.username)  nickname,  \n" +
            "            IF(p.is_nameless = TRUE, NULL, p.account) ACCOUNT,  \n" +
            "            p.topic_id topicId, pt.title topicTitle, p.is_nameless isNameless, p.create_time createTime,  \n" +
            "            pd.content_text content, pd.word_num wordNum, pd.like_num likeNum, pd.view_num viewNum, pd.comment_num commentNum,  \n" +
            "            pd.collect_num collectNum, pd.share_num shareNum,  \n" +
            "            IF(pl.post_id IS NULL , FALSE, TRUE) isLike  \n" +
            "            FROM post p  \n" +
            "            LEFT JOIN post_data pd ON p.id = pd.post_id  \n" +
            "            LEFT JOIN post_topics pt ON p.topic_id = pt.id  \n" +
            "            LEFT JOIN post_like pl ON p.id = pl.post_id  \n" +
            "            LEFT JOIN `user` u ON u.account =  p.account\n" +
            "            WHERE p.is_die = FALSE  \n" +
            "            AND p.id < #{lastPostId} " +
            "            ORDER BY p.create_time DESC, p.id DESC " +
            "            LIMIT #{pageSize}")

    List<IndexShowPostVo> getPostWithData(@Param("lastPostId") Integer lastPostId, @Param("pageSize") int pageSize);

    @Select("SELECT p.id id, p.avatar avatar, p.nickname nickname, " +
            "IF(p.is_nameless = true, NULL, p.account) account, " +
            "p.topic_id topicId, pt.title topicTitle, p.is_nameless isNameless, p.create_time createTime, " +
            "pd.content_text content, pd.word_num wordNum, pd.like_num likeNum, pd.view_num viewNum, pd.comment_num commentNum, " +
            "pd.collect_num collectNum, pd.share_num shareNum, " +
            "IF(pl.post_id is null , false, true) isLike " +
            "FROM post p " +
            "LEFT JOIN post_data pd ON p.id = pd.post_id " +
            "LEFT JOIN post_topics pt ON p.topic_id = pt.id " +
            "LEFT JOIN post_like pl ON p.id = pl.post_id " +
            "WHERE p.is_die = false " +
            "AND p.account = #{account} " +
            "ORDER BY p.create_time DESC, p.id DESC ")

    List<IndexShowPostVo> listMyPosts(@Param("account") Long account);



    @Select("SELECT p.id, p.avatar, p.nickname, p.account, p.topic_id, p.is_nameless, " +
            "p.create_time, p.is_die, d.content_text, d.weight, d.word_num, d.like_num , " +
            "d.comment_num, d.collect_num, d.share_num, d.view_num " +
            "FROM post p " +
            "LEFT JOIN post_data d ON p.id = d.post_id")
    List<PostES> selectPostDataForES();

    // 在 PostMapper 接口中添加以下方法
    @Select("SELECT p.id id, IF(p.is_nameless = TRUE , p.avatar, u.avatar) avatar, " +
            "IF(p.is_nameless = TRUE , p.nickname, u.username) nickname, " +
            "IF(p.is_nameless = TRUE, NULL, p.account) account, " +
            "p.topic_id topicId, pt.title topicTitle, p.is_nameless isNameless, p.create_time createTime, " +
            "pd.content_text content, pd.word_num wordNum, pd.like_num likeNum, pd.view_num viewNum, " +
            "pd.comment_num commentNum, pd.collect_num collectNum, pd.share_num shareNum, pd.heat heat, " +
            "IF(pl.post_id IS NULL , FALSE, TRUE) isLike " +
            "FROM post p " +
            "LEFT JOIN post_data pd ON p.id = pd.post_id " +
            "LEFT JOIN post_topics pt ON p.topic_id = pt.id " +
            "LEFT JOIN post_like pl ON p.id = pl.post_id AND pl.account = #{account} " +
            "LEFT JOIN `user` u ON u.account = p.account " +
            "WHERE p.is_die = FALSE " +
            "ORDER BY pd.heat DESC, p.create_time DESC " +
            "LIMIT #{limit}")
    List<IndexShowPostVo> getHotPosts(@Param("account") Long account, @Param("limit") int limit);
}
